<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/4/5 0005
 * Time: 2:00
 */
namespace Admin\Controller;

use Think\Controller;
use Think\Verify;

class LoginController extends Controller
{

    public function index()
    {
        $this->display();
    }

    public function login()
    {
        $UserClass = I("post.UserClass");
        $username = I("post.username");
        $password = I("post.password");
        $yzm = I("post.yzm");

        if ($this->check_verify($yzm)) {
            if ($UserClass == 0) {
                $AdminInfo = D("AdminInfo");
                $ret = $AdminInfo->checkUser($username, $password);
                if ($ret == 1) {
                    $retInfo = $AdminInfo->getItem($username);
                    session("UserId", $retInfo["adminid"]);
                    session("Name", $retInfo["name"]);
                    session("Rank", $retInfo["rank"]);
                    $this->success("登录成功！", U("Admin/Index/index"));
                } elseif ($ret == 0) {
                    $this->error("密码错误！");
                } elseif ($ret == -1) {
                    $this->error("此账户不存在！");
                }
            } elseif ($UserClass == 1) {
                $TecInfo = D("tecInfo");
                $ret = $TecInfo->checkUser($username, $password);
                if ($ret == 1) {
                    $retInfo = $TecInfo->getItem($username);
                    session("UserId", $retInfo["tecid"]);
                    session("Name", $retInfo["name"]);
                    session("DeptId", $retInfo["deptid"]);
                    session("Rank", $retInfo["rank"]);
                    $this->success("登录成功！", U("Admin/Index/index"));
                } elseif ($ret == 0) {
                    $this->error("密码错误！");
                } elseif ($ret == -1) {
                    $this->error("此账户不存在！");
                }
            }
        } else {
            $this->error("验证码不正确！");
        }
    }

    public function logout()
    {
        session(null);
        $this->success("退出成功！", U("Admin/Login/index"));
    }

    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function ver()
    {
        $Verify = new Verify();
        $Verify->reset = true;
        $Verify->length = 4;
        $Verify->entry();
    }
}